package com.test.demos.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.test.demos.pojo.dto.AppointmentsDTO;
import com.test.demos.pojo.entity.Appointments;
import com.test.demos.service.AppointmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.test.demos.utils.Result;

/**
 * 体检预约
 *
 * @author twt
 * @since 2025-04-14 16:25:35
 */
@CrossOrigin
@RestController
@RequestMapping("appointments")
public class AppointmentsController {

    /**
     * 服务对象
     */
    @Autowired
    private AppointmentsService appointmentsService;

    /**
     * 通过ID查询单条数据
     *
     * @param appointmentId 主键
     * @return 实例对象
     */
    @GetMapping("{id}")
    public Result<Appointments> queryById(@PathVariable("id") Integer appointmentId) {
        return Result.ok(appointmentsService.queryById(appointmentId));
    }

    /**
     * 预约套餐
     *
     */
    @SaCheckLogin
    @PostMapping()
    public Result booking(@RequestBody AppointmentsDTO dto) {
        dto.setUserId(StpUtil.getLoginIdAsInt());
        return appointmentsService.booking(dto);
    }
    /**
     * 我的预约
     */
    @SaCheckLogin
    @GetMapping("/me")
    public Result myAppointments() {
        if (!StpUtil.isLogin()) {
            return Result.error("未登录");
        }
        Object loginIdDefaultNull = StpUtil.getLoginIdDefaultNull();
        if (loginIdDefaultNull == null) {
            return Result.error("未登录");
        }
        Integer loginId = Integer.valueOf((String)loginIdDefaultNull );
        if (loginId == null) {
            return Result.error("未登录");
        }
        return Result.ok(appointmentsService.myAppointments(loginId));
    }


    /**
     * 查询全部数据
     */
    @SaCheckLogin
    @GetMapping()
    public Result queryAll() {
        return Result.ok(appointmentsService.queryAll());
    }
    /**
     * 取消预约
     *
     * @param appointmentId 主键
     * @return 是否成功
     */
    @SaCheckLogin
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id") Integer appointmentId) {
        appointmentsService.deleteById(appointmentId);
        return Result.ok();
    }
}
